package cn.wuxing.mediation.system.service.impl;


import cn.wuxing.mediation.common.core.domain.entity.SysUser;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.system.domain.SysDisputeType;
import cn.wuxing.mediation.system.domain.vo.*;
import cn.wuxing.mediation.system.mapper.IndexDisputeEventMapper;
import cn.wuxing.mediation.system.mapper.StatisticsMapper;
import cn.wuxing.mediation.system.mapper.SysDisputeTypeMapper;
import cn.wuxing.mediation.system.service.ISysUserService;
import cn.wuxing.mediation.system.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

    @Resource
    private IndexDisputeEventMapper indexDisputeEventMapper;

    @Resource
    private SysDisputeTypeMapper sysDisputeTypeMapper;

    @Autowired
    private ISysUserService userService;


    //纠纷案件总况分析
    @Override
    public List<Object> overallSituation(String source,Integer eventType, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> overallList = new LinkedHashMap<>();
        overallList.put("0","案件数量");
        overallList.put("1","待分派数");//待分派数
        overallList.put("2","已分派数");//已分派数
        overallList.put("3","待受理数");//待受理数
        overallList.put("4","已受理数");//已受理数
        overallList.put("5","待调解数");//待调解数
        overallList.put("6","已调解数");//已调解数
        overallList.put("7","调解成功数");//调解成功数
        overallList.put("8","调解成功率");//调解成功率
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }

        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }

        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            String WindowName = "";
            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : overallList.entrySet()){
                String key = entry.getKey();
                String value = entry.getValue();
                int eventState = 0;
                if (key.equals("1")){
                    eventState  = 1;//待分派数
                }else if (key.equals("2")){
                    eventState  = 3;//已分派数 待受理
                }else if (key.equals("3")){
                    eventState  = 3;//待受理数
                }else if (key.equals("4")){
                    eventState  = 4;//已受理数 待调解
                }else if (key.equals("5")){
                    eventState  = 4;//待调解数
                }else if (key.equals("6")){
                    eventState  = 8;//已调解数 待结案
                }

                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (!key.equals("0")){
                    cmNotDisputeEventVo.setEventState(eventState);
                }
                if (source.equals("1")){
                    if (eventType!=null){
                        cmNotDisputeEventVo.setEventType(eventType);
                    }
                }
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                //查询调解成功数
                if (key.equals("7")){
                    cmNotDisputeEventVo.setEventState(null);
                    cmNotDisputeEventVo.setMediationResult("1");//调解成功
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numf));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmNotDisputeEventVo.setDaiyiZong("8");
                    cmNotDisputeEventVo.setEventState(null);
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numf));
                    }
                } else {
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numf));
                    }
                }


                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);
                if (!key.equals("0")){
                    cmOrdinaryDisputeEventVo.setEventState(eventState);
                }
                if (source.equals("2")){
                    if (eventType!=null){
                        cmOrdinaryDisputeEventVo.setEventType(eventType);
                    }
                }
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                //查询调解成功数
                if (key.equals("7")){
                    cmOrdinaryDisputeEventVo.setMediationResult("1");//调解成功
                    cmOrdinaryDisputeEventVo.setEventState(null);
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmOrdinaryDisputeEventVo.setDaiyiZong("8");
                    cmOrdinaryDisputeEventVo.setEventState(null);
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }
                } else {
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,numy ));
                    }
                }


                /*诉前*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);
                if (!key.equals("0")){
                    cmPreLitigationMediationEventVo.setEventState(eventState);
                }
                if (source.equals("3")){
                    if (eventType!=null){
                        cmPreLitigationMediationEventVo.setEventType(eventType);
                    }
                }
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                //查询调解成功数
                if (key.equals("7")){
                    cmPreLitigationMediationEventVo.setMediationResult("1");//调解成功
                    cmPreLitigationMediationEventVo.setEventState(null);
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmPreLitigationMediationEventVo.setDaiyiZong("8");
                    cmPreLitigationMediationEventVo.setEventState(null);
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }
                } else {
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(townId,townName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }else {
                        records.add(new DataRecord(townId,townName, value,nums ));
                    }
                }
            }
        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //纠纷类型分析
    @Override
    public List<Object> typeStatistics(String source, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> overallList = new LinkedHashMap<>();
        overallList.put("0","案件数量");
        overallList.put("1","待分派数");//待分派数
        overallList.put("2","已分派数");//已分派数
        overallList.put("3","待受理数");//待受理数
        overallList.put("4","已受理数");//已受理数
        overallList.put("5","待调解数");//待调解数
        overallList.put("6","已调解数");//已调解数
        overallList.put("7","调解成功数");//调解成功数
        overallList.put("8","调解成功率");//调解成功率
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*非纠纷窗口*/
        IcWindowVo icWindowVo = new IcWindowVo();
        icWindowVo.setDelFlag("0");
        List<IcWindowVo> icWindowVoList = statisticsMapper.selectIcWindowList(icWindowVo);
        for (IcWindowVo window : icWindowVoList) {
            Map<String, Object> map = new HashMap<>();
            map.put("type", "window");  // 指定类型
            map.put("id", window.getId());
            map.put("name", window.getWindowName());  // 假定有这些属性
            getAreaTownList.add(map);
        }
        /*一般纠纷类型*/
        SysDisputeType disputeType = new SysDisputeType();
        disputeType.setEventType("0");
        disputeType.setDisputeStatus("0");
        List<SysDisputeType> disputeTypeList = sysDisputeTypeMapper.selectDisputeTypeList(disputeType);
        for (SysDisputeType type : disputeTypeList) {
            Map<String, Object> map = new HashMap<>();
            map.put("type", "generalDispute");  // 指定类型
            map.put("id", type.getDisputeId());
            map.put("name", type.getDisputeName());  // 假定有这些属性
            getAreaTownList.add(map);
        }

        /*诉前纠纷类型*/
        SysDisputeType disputeTypetwo = new SysDisputeType();
        disputeTypetwo.setEventType("1");
        disputeType.setDisputeStatus("0");
        List<SysDisputeType> disputeTypetwoList = sysDisputeTypeMapper.selectDisputeTypeList(disputeTypetwo);
        for (SysDisputeType typeTwo : disputeTypetwoList) {
            Map<String, Object> map = new HashMap<>();
            map.put("type", "preLitigationDispute");  // 指定类型
            map.put("id", typeTwo.getDisputeId());
            map.put("name", typeTwo.getDisputeName());  // 假定有这些属性
            getAreaTownList.add(map);
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();

        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long typeId = 0L;
            String typeName = "";
                    typeId = (Long) area.get("id"); // id;
                    typeName = (String) area.get("name"); // name


            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : overallList.entrySet()){
                String key = entry.getKey();
                String value = entry.getValue();
                int eventState = 0;
                if (key.equals("1")){
                    eventState  = 1;//待分派数
                }else if (key.equals("2")){
                    eventState  = 3;//已分派数 待受理
                }else if (key.equals("3")){
                    eventState  = 3;//待受理数
                }else if (key.equals("4")){
                    eventState  = 4;//已受理数 待调解
                }else if (key.equals("5")){
                    eventState  = 4;//待调解数
                }else if (key.equals("6")){
                    eventState  = 8;//已调解数 待结案
                }

                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                cmNotDisputeEventVo.setDraftFlag(0);
                cmNotDisputeEventVo.setTypeName(typeName);
                if (!key.equals("0")){
                    cmNotDisputeEventVo.setEventState(eventState);
                }
                if (startTime != null && startTime!=""){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                if (level != null && level!=""){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(Id));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(Id));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(Id));
                    }
                }else {
                    if (!String.valueOf(Id).equals("null")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(Id));
                    }
                }
                //查询调解成功数
                if (key.equals("7")){
                    cmNotDisputeEventVo.setEventState(null);
                    cmNotDisputeEventVo.setMediationResult("1");//调解成功
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmNotDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmNotDisputeEventVo.setDaiyiZong("8");
                    cmNotDisputeEventVo.setEventState(null);
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmNotDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }
                } else {
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmNotDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numf));
                    }
                }


                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);
                cmOrdinaryDisputeEventVo.setTypeName(typeName);
                if (!key.equals("0")){
                    cmOrdinaryDisputeEventVo.setEventState(eventState);
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }

                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(Id));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(Id));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(Id));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(Id));
                }
                //查询调解成功数
                if (key.equals("7")){
                    cmOrdinaryDisputeEventVo.setMediationResult("1");//调解成功
                    cmOrdinaryDisputeEventVo.setEventState(null);
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmOrdinaryDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmOrdinaryDisputeEventVo.setDaiyiZong("8");
                    cmOrdinaryDisputeEventVo.setEventState(null);
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmOrdinaryDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }
                } else {
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmOrdinaryDisputeEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,numy ));
                    }
                }


                /*诉前*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);
                cmPreLitigationMediationEventVo.setTypeName(typeName);
                if (!key.equals("0")){
                    cmPreLitigationMediationEventVo.setEventState(eventState);
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(Id));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(Id));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(Id));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(Id));
                }
                //查询调解成功数
                if (key.equals("7")){

                    cmPreLitigationMediationEventVo.setMediationResult("1");//调解成功
                    cmPreLitigationMediationEventVo.setEventState(null);
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmPreLitigationMediationEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }

                }else if (key.equals("8")){//计算调解成功率
                    //先查询待结案 已结案的总和
                    cmPreLitigationMediationEventVo.setDaiyiZong("8");
                    cmPreLitigationMediationEventVo.setEventState(null);
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmPreLitigationMediationEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }
                } else {
                    if (areaId != null && areaId != 0) {
                        if (areaId!=50){
                            //areaId不等于50则说明数据为地区数据
                            cmPreLitigationMediationEventVo.setTownId(String.valueOf(areaId));
                        }
                    }
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("2")){
                        records.add(new DataRecord(typeId,typeName, value,0 ));
                    }else if (source.equals("3")){
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }else {
                        records.add(new DataRecord(typeId,typeName, value,nums ));
                    }
                }
            }
        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }


    //纠纷区域分析
    @Override
    public List<Object> regionalAnalysis(String source, String level,String startTime,String endTime,Long Id) {
        /*非纠纷窗口*/
        IcWindowVo icWindowVo = new IcWindowVo();
        icWindowVo.setDelFlag("0");
        List<IcWindowVo> icWindowVoList = statisticsMapper.selectIcWindowList(icWindowVo);

        /*一般纠纷类型*/
        SysDisputeType disputeType = new SysDisputeType();
        disputeType.setEventType("0");
        List<SysDisputeType> disputeTypeList = sysDisputeTypeMapper.selectDisputeTypeList(disputeType);

        /*诉前纠纷类型*/
        SysDisputeType disputeTypetwo = new SysDisputeType();
        disputeTypetwo.setEventType("1");
        List<SysDisputeType> disputeTypetwoList = sysDisputeTypeMapper.selectDisputeTypeList(disputeTypetwo);

        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            String WindowName = "";
            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (IcWindowVo icWindowVo1 : icWindowVoList){
                WindowName = icWindowVo1.getWindowName();
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                cmNotDisputeEventVo.setTypeName(icWindowVo1.getWindowName());
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }

                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                numf = cmNotDisputeEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, WindowName,numf ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, WindowName,0 ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, WindowName,0 ));
                }else {
                    records.add(new DataRecord(townId,townName, WindowName,numf ));
                }

            }
            String disputeName = "";
            for (SysDisputeType sysDisputeType :disputeTypeList){
                disputeName = sysDisputeType.getDisputeName();
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);
                cmOrdinaryDisputeEventVo.setTypeName(disputeName);
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }

                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                numy = cmOrdinaryDisputeEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, disputeName,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, disputeName,numy ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, disputeName,0 ));
                }else {
                    records.add(new DataRecord(townId,townName, disputeName,numy ));
                }

            }
            String disputeNameTwo = "";
            for (SysDisputeType sysDisputeType :disputeTypetwoList){
                disputeNameTwo = sysDisputeType.getDisputeName();
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);
                cmPreLitigationMediationEventVo.setTypeName(disputeNameTwo);
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                nums = cmPreEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, disputeNameTwo,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, disputeNameTwo,0 ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, disputeNameTwo,nums ));
                }else {
                    records.add(new DataRecord(townId,townName, disputeNameTwo,nums ));
                }

            }
        }
        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }
        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //纠纷主体分析
    @Override
    public List<Object> subjectStatistics(String source, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> subjectList = new LinkedHashMap<>();
        subjectList.put("0","总数");
        subjectList.put("1","自然人");//自然人
        subjectList.put("2","法人组织");//法人组织
        subjectList.put("3","非法人组织");//非法人组织
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            String WindowName = "";
            int numf = 0;
            int numy = 0;
            int nums = 0;
            int numall = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : subjectList.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (!key.equals("0")) {
                    cmNotDisputeEventVo.setApplicantType(key);
                }
                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()) {
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
//                if (source != null && !source.equals(""))  {
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, numf));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else {
                        records.add(new DataRecord(townId, townName, value, numf));
                    }


                    CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                    cmOrdinaryDisputeEventVo.setDraftFlag(0);
                    if (!key.equals("0")) {
                        cmOrdinaryDisputeEventVo.setApplicantType(key);
                    }
                    if (level != null && !level.isEmpty()) {
                        if (level.equals("1")) {
                            cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                        } else if (level.equals("2")) {
                            cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                        } else if (level.equals("3")) {
                            cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                        }
                    } else {
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }

                    if (startTime != null && !startTime.isEmpty()) {
                        cmOrdinaryDisputeEventVo.setStartTime(startTime);
                        cmOrdinaryDisputeEventVo.setEndTime(endTime);
                    }
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, numy));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else {
                        records.add(new DataRecord(townId, townName, value, numy));
                    }


                    CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                    cmPreLitigationMediationEventVo.setDraftFlag(0);
                    if (!key.equals("0")) {
                        cmPreLitigationMediationEventVo.setApplicantType(key);
                    }
                    if (level != null && !level.isEmpty()) {
                        if (level.equals("1")) {
                            cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                        } else if (level.equals("2")) {
                            cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                        } else if (level.equals("3")) {
                            cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                        }
                    } else {
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }

                    if (startTime != null && !startTime.isEmpty()) {
                        cmPreLitigationMediationEventVo.setStartTime(startTime);
                        cmPreLitigationMediationEventVo.setEndTime(endTime);
                    }
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, nums));
                    } else {
                        records.add(new DataRecord(townId, townName, value, nums));
                    }
//                 } else {
//                    CmAllEventVo cmAllEventVo = new CmAllEventVo();
//                    cmAllEventVo.setDraftFlag(0);
//                    if (!key.equals("0")) {
//                        cmAllEventVo.setApplicantType(key);
//                    }
//                    if (level != null && !level.isEmpty()) {
//                        if (level.equals("1")) {
//                            cmAllEventVo.setTownId(String.valueOf(townId));
//                        } else if (level.equals("2")) {
//                            cmAllEventVo.setVillageId(String.valueOf(townId));
//                        } else if (level.equals("3")) {
//                            cmAllEventVo.setHouseId(String.valueOf(townId));
//                        }
//                    } else {
//                        cmAllEventVo.setTownId(String.valueOf(townId));
//                    }
//
//                    if (startTime != null && !startTime.isEmpty()) {
//                        cmAllEventVo.setStartTime(startTime);
//                        cmAllEventVo.setEndTime(endTime);
//                    }
//                    if (value.equals("自然人")||value.equals("总数")){
//                        cmAllEventVo.setAllTypeCard("0");
//                    }
//                    List<CmAllEventVo> cmAllEventVoList = statisticsMapper.cmAllEventVoSelList(cmAllEventVo);
//                    numall = cmAllEventVoList.size();
//                    records.add(new DataRecord(townId, townName, value, numall));
//                }


            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //纠纷趋势分析
    @Override
    public List<Object> trendStatistics(String dayNum,String source,Integer eventType, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把获取的日期数据存储到list集合里面 便于后面循环调用*/
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取前7天的日期
        LocalDate sevenDaysAgo = today.minusDays(7);
        System.out.println("当前日期前7天: " + sevenDaysAgo);

        // 获取前15天的日期
        LocalDate fifteenDaysAgo = today.minusDays(15);
        System.out.println("当前日期前15天: " + fifteenDaysAgo);

        // 获取前30天的日期
        LocalDate thirtyDaysAgo = today.minusDays(30);
        System.out.println("当前日期前30天: " + thirtyDaysAgo);

        int days = 7;
        if (dayNum!=null){
            days = Integer.parseInt(dayNum);
        }
        // 获取当前日期及前X天的每天日期
        System.out.println("最近X天的日期:");
        List<LocalDate> lastXDays = getLastNDays(days, today);

        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (LocalDate date : lastXDays){
                String data = String.valueOf(date);
                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                cmNotDisputeEventVo.setDraftFlag(0);
                cmNotDisputeEventVo.setTime(data);
                //地区等级
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                if (source.equals("1")){
                    if (eventType!=null){
                        cmNotDisputeEventVo.setEventType(eventType);
                    }
                }
                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                numf = cmNotDisputeEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, data,numf));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, data,0));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, data,0));
                }else {
                    records.add(new DataRecord(townId,townName, data,numf));
                }



                /*一般纠纷件*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);
                cmOrdinaryDisputeEventVo.setTime(data);
                //地区等级
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                if (source.equals("2")){
                    if (eventType!=null){
                        cmOrdinaryDisputeEventVo.setEventType(eventType);
                    }
                }
                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                numy = cmOrdinaryDisputeEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, data,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, data,numy ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, data,0 ));
                }else {
                    records.add(new DataRecord(townId,townName, data,numy ));
                }



                /*诉前纠纷件*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);
                cmPreLitigationMediationEventVo.setTime(data);
                //地区等级
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                if (source.equals("3")){
                    if (eventType!=null){
                        cmPreLitigationMediationEventVo.setEventType(eventType);
                    }
                }
                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                nums = cmPreEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, data,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, data,0 ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, data,nums ));
                }else {
                    records.add(new DataRecord(townId,townName, data,nums ));
                }


            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //纠纷涉事人员年龄段分析
    @Override
    public List<Object> ageStatistics(String source, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> ageList = new LinkedHashMap<>();
        ageList.put("0","纠纷总数");
        ageList.put("1","18岁及以下");//18岁及以下
        ageList.put("2","19-29岁");//19-29岁
        ageList.put("3","30-39岁");//30-39岁
        ageList.put("4","40-59岁");//30-39岁
        ageList.put("5","60-79岁");//30-39岁
        ageList.put("6","80岁及以上");//30-39岁
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : ageList.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
//                if (source != null && !source.equals("")) {
                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (key.equals("0")) {
                    cmNotDisputeEventVo.setAgeStart("0");
                    cmNotDisputeEventVo.setAgeEnd("999");
                } else if (key.equals("1")) {
                    cmNotDisputeEventVo.setAgeStart("0");
                    cmNotDisputeEventVo.setAgeEnd("18");
                } else if (key.equals("2")) {
                    cmNotDisputeEventVo.setAgeStart("19");
                    cmNotDisputeEventVo.setAgeEnd("29");
                } else if (key.equals("3")) {
                    cmNotDisputeEventVo.setAgeStart("30");
                    cmNotDisputeEventVo.setAgeEnd("39");
                } else if (key.equals("4")) {
                    cmNotDisputeEventVo.setAgeStart("40");
                    cmNotDisputeEventVo.setAgeEnd("59");
                } else if (key.equals("5")) {
                    cmNotDisputeEventVo.setAgeStart("60");
                    cmNotDisputeEventVo.setAgeEnd("79");
                } else if (key.equals("6")) {
                    cmNotDisputeEventVo.setAgeStart("80");
                    cmNotDisputeEventVo.setAgeEnd("999");
                }

                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()) {
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoList(cmNotDisputeEventVo);
                numf = cmNotDisputeEventVoList.size();
                if (source.equals("1")) {
                    records.add(new DataRecord(townId, townName, value, numf));
                } else if (source.equals("2")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else if (source.equals("3")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else {
                    records.add(new DataRecord(townId, townName, value, numf));
                }



                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);
                if (key.equals("0")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("0");
                    cmOrdinaryDisputeEventVo.setAgeEnd("999");
                } else if (key.equals("1")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("0");
                    cmOrdinaryDisputeEventVo.setAgeEnd("18");
                } else if (key.equals("2")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("19");
                    cmOrdinaryDisputeEventVo.setAgeEnd("29");
                } else if (key.equals("3")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("30");
                    cmOrdinaryDisputeEventVo.setAgeEnd("39");
                } else if (key.equals("4")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("40");
                    cmOrdinaryDisputeEventVo.setAgeEnd("59");
                } else if (key.equals("5")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("60");
                    cmOrdinaryDisputeEventVo.setAgeEnd("79");
                } else if (key.equals("6")) {
                    cmOrdinaryDisputeEventVo.setAgeStart("80");
                    cmOrdinaryDisputeEventVo.setAgeEnd("999");
                }

                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()) {
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                numy = cmOrdinaryDisputeEventVoList.size();
                if (source.equals("1")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else if (source.equals("2")) {
                    records.add(new DataRecord(townId, townName, value, numy));
                } else if (source.equals("3")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else {
                    records.add(new DataRecord(townId, townName, value, numy));
                }


                /*诉前调解件*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);
                if (key.equals("0")) {
                    cmPreLitigationMediationEventVo.setAgeStart("0");
                    cmPreLitigationMediationEventVo.setAgeEnd("999");
                } else if (key.equals("1")) {
                    cmPreLitigationMediationEventVo.setAgeStart("0");
                    cmPreLitigationMediationEventVo.setAgeEnd("18");
                } else if (key.equals("2")) {
                    cmPreLitigationMediationEventVo.setAgeStart("19");
                    cmPreLitigationMediationEventVo.setAgeEnd("29");
                } else if (key.equals("3")) {
                    cmPreLitigationMediationEventVo.setAgeStart("30");
                    cmPreLitigationMediationEventVo.setAgeEnd("39");
                } else if (key.equals("4")) {
                    cmPreLitigationMediationEventVo.setAgeStart("40");
                    cmPreLitigationMediationEventVo.setAgeEnd("59");
                } else if (key.equals("5")) {
                    cmPreLitigationMediationEventVo.setAgeStart("60");
                    cmPreLitigationMediationEventVo.setAgeEnd("79");
                } else if (key.equals("6")) {
                    cmPreLitigationMediationEventVo.setAgeStart("80");
                    cmPreLitigationMediationEventVo.setAgeEnd("999");
                }
                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()) {
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                nums = cmPreEventVoList.size();
                if (source.equals("1")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else if (source.equals("2")) {
                    records.add(new DataRecord(townId, townName, value, 0));
                } else if (source.equals("3")) {
                    records.add(new DataRecord(townId, townName, value, nums));
                } else {
                    records.add(new DataRecord(townId, townName, value, nums));
                }

//            }else {
//                    CmAllEventVo cmAllEventVo = new CmAllEventVo();
//                    if (key.equals("0")) {
//                        cmAllEventVo.setAgeStart("0");
//                        cmAllEventVo.setAgeEnd("999");
//                    } else if (key.equals("1")) {
//                        cmAllEventVo.setAgeStart("0");
//                        cmAllEventVo.setAgeEnd("18");
//                    } else if (key.equals("2")) {
//                        cmAllEventVo.setAgeStart("19");
//                        cmAllEventVo.setAgeEnd("29");
//                    } else if (key.equals("3")) {
//                        cmAllEventVo.setAgeStart("30");
//                        cmAllEventVo.setAgeEnd("39");
//                    } else if (key.equals("4")) {
//                        cmAllEventVo.setAgeStart("40");
//                        cmAllEventVo.setAgeEnd("59");
//                    } else if (key.equals("5")) {
//                        cmAllEventVo.setAgeStart("60");
//                        cmAllEventVo.setAgeEnd("79");
//                    } else if (key.equals("6")) {
//                        cmAllEventVo.setAgeStart("80");
//                        cmAllEventVo.setAgeEnd("999");
//                    }
//
//                    if (level != null && !level.isEmpty()) {
//                        if (level.equals("1")) {
//                            cmAllEventVo.setTownId(String.valueOf(townId));
//                        } else if (level.equals("2")) {
//                            cmAllEventVo.setVillageId(String.valueOf(townId));
//                        } else if (level.equals("3")) {
//                            cmAllEventVo.setHouseId(String.valueOf(townId));
//                        }
//                    } else {
//                        cmAllEventVo.setTownId(String.valueOf(townId));
//                    }
//                    if (startTime != null && !startTime.isEmpty()) {
//                        cmAllEventVo.setStartTime(startTime);
//                        cmAllEventVo.setEndTime(endTime);
//                    }
//                    List<CmAllEventVo> cmAllEventVoList = statisticsMapper.cmAllEventVoSelList(cmAllEventVo);
//                    numf = cmAllEventVoList.size();
//                    records.add(new DataRecord(townId, townName, value, numf));
//                }

            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //纠纷涉事人员性别分析
    @Override
    public List<Object> sexStatistics(String source,Integer eventType, String level,String startTime,String endTime,Long Id) {
        /*总数、自然人、法人组织、非法人组织*/
        /*先把数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> ageList = new LinkedHashMap<>();
        ageList.put("0","纠纷总数");
        ageList.put("1","男性");//男性
        ageList.put("2","女性");//女性
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : ageList.entrySet()){
                String key = entry.getKey();
                String value = entry.getValue();

//                if (source != null && !source.equals("")) {
                    /*非纠纷*/
                    CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                    if (key.equals("0")) {
                        cmNotDisputeEventVo.setSexZong("0");
                    } else if (key.equals("1")) {
                        cmNotDisputeEventVo.setSex("1");
                    } else if (key.equals("2")) {
                        cmNotDisputeEventVo.setSex("2");
                    }
                    if (level != null && !level.isEmpty()) {
                        if (level.equals("1")) {
                            cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                        } else if (level.equals("2")) {
                            cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                        } else if (level.equals("3")) {
                            cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                        }
                    } else {
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }
                    if (startTime != null && !startTime.isEmpty()) {
                        cmNotDisputeEventVo.setStartTime(startTime);
                        cmNotDisputeEventVo.setEndTime(endTime);
                    }
                    if (source.equals("1")) {
                        if (eventType != null) {
                            cmNotDisputeEventVo.setEventType(eventType);
                        }
                    }
                    List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoList(cmNotDisputeEventVo);
                    numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, numf));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else {
                        records.add(new DataRecord(townId, townName, value, numf));
                    }



                    /*一般纠纷*/
                    CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                    cmOrdinaryDisputeEventVo.setDraftFlag(0);
                    if (key.equals("0")) {
                        cmOrdinaryDisputeEventVo.setSexZong("0");
                    } else if (key.equals("1")) {
                        cmOrdinaryDisputeEventVo.setSex("1");
                    } else if (key.equals("2")) {
                        cmOrdinaryDisputeEventVo.setSex("2");
                    }

                    if (level != null && !level.isEmpty()) {
                        if (level.equals("1")) {
                            cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                        } else if (level.equals("2")) {
                            cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                        } else if (level.equals("3")) {
                            cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                        }
                    } else {
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }

                    if (startTime != null && !startTime.isEmpty()) {
                        cmOrdinaryDisputeEventVo.setStartTime(startTime);
                        cmOrdinaryDisputeEventVo.setEndTime(endTime);
                    }

                    if (source.equals("2")) {
                        if (eventType != null) {
                            cmOrdinaryDisputeEventVo.setEventType(eventType);
                        }
                    }
                    List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                    numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, numy));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else {
                        records.add(new DataRecord(townId, townName, value, numy));
                    }


                    /*诉前调解件*/
                    CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                    cmPreLitigationMediationEventVo.setDraftFlag(0);
                    if (key.equals("0")) {
                        cmPreLitigationMediationEventVo.setSexZong("0");
                    } else if (key.equals("1")) {
                        cmPreLitigationMediationEventVo.setSex("1");
                    } else if (key.equals("2")) {
                        cmPreLitigationMediationEventVo.setSex("2");
                    }
                    if (level != null && !level.isEmpty()) {
                        if (level.equals("1")) {
                            cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                        } else if (level.equals("2")) {
                            cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                        } else if (level.equals("3")) {
                            cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                        }
                    } else {
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }

                    if (startTime != null && !startTime.isEmpty()) {
                        cmPreLitigationMediationEventVo.setStartTime(startTime);
                        cmPreLitigationMediationEventVo.setEndTime(endTime);
                    }

                    if (source.equals("3")) {
                        if (eventType != null) {
                            cmPreLitigationMediationEventVo.setEventType(eventType);
                        }
                    }
                    List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                    nums = cmPreEventVoList.size();
                    if (source.equals("1")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("2")) {
                        records.add(new DataRecord(townId, townName, value, 0));
                    } else if (source.equals("3")) {
                        records.add(new DataRecord(townId, townName, value, nums));
                    } else {
                        records.add(new DataRecord(townId, townName, value, nums));
                    }
//                }else{
//                    CmAllEventVo cmAllEventVo = new CmAllEventVo();
//                    if (key.equals("0")) {
//                        cmAllEventVo.setSexZong("0");
//                    } else if (key.equals("1")) {
//                        cmAllEventVo.setSex("1");
//                    } else if (key.equals("2")) {
//                        cmAllEventVo.setSex("2");
//                    }
//                    if (level != null && !level.isEmpty()) {
//                        if (level.equals("1")) {
//                            cmAllEventVo.setTownId(String.valueOf(townId));
//                        } else if (level.equals("2")) {
//                            cmAllEventVo.setVillageId(String.valueOf(townId));
//                        } else if (level.equals("3")) {
//                            cmAllEventVo.setHouseId(String.valueOf(townId));
//                        }
//                    } else {
//                        cmAllEventVo.setTownId(String.valueOf(townId));
//                    }
//                    if (startTime != null && !startTime.isEmpty()) {
//                        cmAllEventVo.setStartTime(startTime);
//                        cmAllEventVo.setEndTime(endTime);
//                    }
//                    if (value.equals("自然人")||value.equals("总数")){
//                        cmAllEventVo.setAllTypeCard("0");
//                    }
//                    List<CmAllEventVo> cmNotDisputeEventVoList = statisticsMapper.cmAllEventVoSelList(cmAllEventVo);
//                    numf = cmNotDisputeEventVoList.size();
//                    records.add(new DataRecord(townId, townName, value, numf));
//                }

            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //时间段高发纠纷类型分析
    @Override
    public List<Object> highTypeStatistics(String dateType, String source, String level,String startTime,String endTime,Long Id)  {
        /*纠纷总数、周、月、年*/
        /*先把时间数据数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> ageList = new LinkedHashMap<>();
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        ageList.put("0","纠纷总数");
        if (dateType != null && !dateType.isEmpty()){
            if (dateType.equals("1")){
                ageList.put("1",getCurrentWeekRange(today));//当前一周区间
                ageList.put("2",getLastWeekRange(today));//前一周区间
                ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
            }else if(dateType.equals("2")){
                ageList.put("1",getCurrentMonthRange(today));//当前月份区间
                ageList.put("2",getLastMonthRange(today));//前一个月份区间
                ageList.put("3",getLastTwoMonthsRange(today));//前两个月份区间
            } else if(dateType.equals("3")){
                ageList.put("1",getCurrentYearRange(today));//当前年份区间
                ageList.put("2",getLastYearRange(today));//前一个月份区间
                ageList.put("3",getTwoYearsAgoRange(today));//前两个月份区间
            }
        } else {
            ageList.put("1",getCurrentWeekRange(today));//当前一周区间
            ageList.put("2",getLastWeekRange(today));//前一周区间
            ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
        }


        /*非纠纷窗口*/
        IcWindowVo icWindowVo = new IcWindowVo();
        icWindowVo.setDelFlag("0");
        List<IcWindowVo> icWindowVoList = statisticsMapper.selectIcWindowList(icWindowVo);

        /*一般纠纷类型*/
        SysDisputeType disputeType = new SysDisputeType();
        disputeType.setEventType("0");
        List<SysDisputeType> disputeTypeList = sysDisputeTypeMapper.selectDisputeTypeList(disputeType);

        /*诉前纠纷类型*/
        SysDisputeType disputeTypetwo = new SysDisputeType();
        disputeTypetwo.setEventType("1");
        List<SysDisputeType> disputeTypetwoList = sysDisputeTypeMapper.selectDisputeTypeList(disputeTypetwo);


        List<DataTypeRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }



            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : ageList.entrySet()){
                List<TypeRecord> typeRecordList = new ArrayList<>();
                List<TypeRecord> typeRecordNotList = new ArrayList<>();
                List<TypeRecord> typeRecordOrdList = new ArrayList<>();
                List<TypeRecord> typeRecordPreList = new ArrayList<>();
                for (IcWindowVo icWindowVo1 :icWindowVoList){
                    TypeRecord typeRecord = new TypeRecord();
                    typeRecord.setId(icWindowVo1.getId());
                    typeRecord.setName(icWindowVo1.getWindowName());
                    typeRecord.setNum(0);
                    typeRecordNotList.add(typeRecord);
                }
                for (SysDisputeType sysDisputeType :disputeTypeList){
                    TypeRecord typeRecord = new TypeRecord();
                    typeRecord.setId(sysDisputeType.getDisputeId());
                    typeRecord.setName(sysDisputeType.getDisputeName());
                    typeRecord.setNum(0);
                    typeRecordOrdList.add(typeRecord);
                }

                for (SysDisputeType sysDisputeType :disputeTypetwoList){
                    TypeRecord typeRecord = new TypeRecord();
                    typeRecord.setId(sysDisputeType.getDisputeId());
                    typeRecord.setName(sysDisputeType.getDisputeName());
                    typeRecord.setNum(0);
                    typeRecordPreList.add(typeRecord);
                }
                String key = entry.getKey();
                String value= entry.getValue();
                /*若key值为1、2、3 则为周期筛选*/
                startTime = null;
                endTime = null;
                if (key.equals("1")||key.equals("2")||key.equals("3")){
                    startTime = before(value);
                    endTime = after(value);
                }
                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }

                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);


                if (source.equals("1")){
                    typeRecordNotList.forEach(type->{
                        List<CmNotDisputeEventVo> collect = cmNotDisputeEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() && vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }else {
                    typeRecordNotList.forEach(type->{
                        List<CmNotDisputeEventVo> collect = cmNotDisputeEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() &&vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }

                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                if (source.equals("2")){
                    typeRecordOrdList.forEach(type->{
                        List<CmOrdinaryDisputeEventVo> collect = cmOrdinaryDisputeEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() &&vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }else {
                    typeRecordOrdList.forEach(type->{
                        List<CmOrdinaryDisputeEventVo> collect = cmOrdinaryDisputeEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() &&vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }


                /*诉前调解件*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }

                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                if (source.equals("3")){
                    typeRecordPreList.forEach(type->{
                        List<CmPreLitigationMediationEventVo> collect = cmPreEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() &&vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }else {
                    typeRecordPreList.forEach(type->{
                        List<CmPreLitigationMediationEventVo> collect = cmPreEventVoList.stream()
                                .filter(vo -> vo.getTypeName() != null && !vo.getTypeName().isEmpty() &&vo.getTypeName().equals(type.getName()))
                                .collect(Collectors.toList());
                        type.setNum(collect.size());
                    });
                }


                typeRecordList.addAll(typeRecordNotList);
                typeRecordList.addAll(typeRecordOrdList);
                typeRecordList.addAll(typeRecordPreList);
                records.add(new DataTypeRecord(townId,townName, value,typeRecordList ));
            }
        }

        Map<String, Map<String, Object>> statistics = new LinkedHashMap<>();
        for (DataTypeRecord record : records) {
            Long id = record.getId();
            String region = record.getRegion();
            String type = record.getType();
            List<TypeRecord>  value = record.getTypeRecordList();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, value);
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Object>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Object> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Object> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //时间段高发纠纷区域分析
    @Override
    public List<Object> highRegionStatistics(String dateType, String source, String level,String startTime,String endTime,Long Id)  {
        /*纠纷总数、周、月、年*/
        /*先把时间数据数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> ageList = new LinkedHashMap<>();
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取各个时间区间
        System.out.println("当前一周区间: " + getCurrentWeekRange(today));
        System.out.println("前一周区间: " + getLastWeekRange(today));
        System.out.println("前两周区间: " + getLastTwoWeeksRange(today));
        /*当前一周区间: 2024-10-14 到 2024-10-20
            前一周区间: 2024-10-07 到 2024-10-13
            前两周区间: 2024-09-30 到 2024-10-13*/
        // 获取各个时间区间
        System.out.println("当前月份区间: " + getCurrentMonthRange(today));
        System.out.println("前一个月份区间: " + getLastMonthRange(today));
        System.out.println("前两个月份区间: " + getLastTwoMonthsRange(today));

        System.out.println("当前一年区间: " +  getCurrentYearRange(today));
        System.out.println("前一年区间: " +  getLastYearRange(today));
        System.out.println("前两年区间: " + getTwoYearsAgoRange(today));

        ageList.put("0","纠纷总数");
        if (dateType != null && !dateType.isEmpty()){
            if (dateType.equals("1")){
                ageList.put("1",getCurrentWeekRange(today));//当前一周区间
                ageList.put("2",getLastWeekRange(today));//前一周区间
                ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
            }else if(dateType.equals("2")){
                ageList.put("1",getCurrentMonthRange(today));//当前月份区间
                ageList.put("2",getLastMonthRange(today));//前一个月份区间
                ageList.put("3",getLastTwoMonthsRange(today));//前两个月份区间
            } else if(dateType.equals("3")){
                ageList.put("1",getCurrentYearRange(today));//当前年份区间
                ageList.put("2",getLastYearRange(today));//前一个月份区间
                ageList.put("3",getTwoYearsAgoRange(today));//前两个月份区间
            }
        } else {
            ageList.put("1",getCurrentWeekRange(today));//当前一周区间
            ageList.put("2",getLastWeekRange(today));//前一周区间
            ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
        }

        
        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            int numf = 0;
            int numy = 0;
            int nums = 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : ageList.entrySet()){
                String key = entry.getKey();
                String value= entry.getValue();
                /*若key值为1、2、3 则为周期筛选*/
                startTime = null;
                endTime = null;
                if (key.equals("1")||key.equals("2")||key.equals("3")){
                    startTime = before(value);
                    endTime = after(value);
                }
                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()){
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                numf = cmNotDisputeEventVoList.size();

                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, value,numf));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, value,0));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, value,0));
                }else {
                    records.add(new DataRecord(townId,townName, value,numf));
                }



                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (townName.equals("织里镇")&&value.equals("纠纷总数")){
                    System.out.println("111");
                }
                if (startTime != null && !startTime.isEmpty()){
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                numy = cmOrdinaryDisputeEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, value,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, value,numy ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, value,0 ));
                }else {
                    records.add(new DataRecord(townId,townName, value,numy ));
                }


                /*诉前调解件*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()){
                    if (level.equals("1")){
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    }else if (level.equals("2")){
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    }else if (level.equals("3")){
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                }else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()){
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                nums = cmPreEventVoList.size();
                if (source.equals("1")){
                    records.add(new DataRecord(townId,townName, value,0 ));
                }else if (source.equals("2")){
                    records.add(new DataRecord(townId,townName, value,0 ));
                }else if (source.equals("3")){
                    records.add(new DataRecord(townId,townName, value,nums ));
                }else {
                    records.add(new DataRecord(townId,townName, value,nums ));
                }


            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            Long id = record.getId();
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    //时间段高发纠纷主体分析
    @Override
    public List<Object> highSubjectStatistics(String dateType, String source, String level,String startTime,String endTime,Long Id) {
        /*纠纷总数、周、月、年*/
        /*先把时间数据数据存储到list集合里面 便于后面循环调用*/
        Map<String,String> ageList = new LinkedHashMap<>();
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取各个时间区间
        System.out.println("当前一周区间: " + getCurrentWeekRange(today));
        System.out.println("前一周区间: " + getLastWeekRange(today));
        System.out.println("前两周区间: " + getLastTwoWeeksRange(today));

        System.out.println("当前月份区间: " + getCurrentMonthRange(today));
        System.out.println("前一个月份区间: " + getLastMonthRange(today));
        System.out.println("前两个月份区间: " + getLastTwoMonthsRange(today));

        System.out.println("当前一年区间: " +  getCurrentYearRange(today));
        System.out.println("前一年区间: " +  getLastYearRange(today));
        System.out.println("前两年区间: " + getTwoYearsAgoRange(today));

        ageList.put("0","纠纷总数");
        if (dateType != null && !dateType.isEmpty()){
            if (dateType.equals("1")){
                ageList.put("1",getCurrentWeekRange(today));//当前一周区间
                ageList.put("2",getLastWeekRange(today));//前一周区间
                ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
            }else if(dateType.equals("2")){
                ageList.put("1",getCurrentMonthRange(today));//当前月份区间
                ageList.put("2",getLastMonthRange(today));//前一个月份区间
                ageList.put("3",getLastTwoMonthsRange(today));//前两个月份区间
            } else if(dateType.equals("3")){
                ageList.put("1",getCurrentYearRange(today));//当前年份区间
                ageList.put("2",getLastYearRange(today));//前一个月份区间
                ageList.put("3",getTwoYearsAgoRange(today));//前两个月份区间
            }
        } else {
            ageList.put("1",getCurrentWeekRange(today));//当前一周区间
            ageList.put("2",getLastWeekRange(today));//前一周区间
            ageList.put("3",getLastTwoWeeksRange(today));//前两周区间
        }

        List<String> subjectList = new ArrayList<>();
        subjectList.add("总数");
        subjectList.add("自然人");
        subjectList.add("法人组织");
        subjectList.add("非法人组织");



        List<DataRecord> records = new ArrayList<>();
        List<Map<String,Object>> getAreaTownList = new ArrayList<>();
        /*最高级地区*/
        List<Map<String,Object>> getAreaTown = indexDisputeEventMapper.getAreaTown();
        /*下一级地区(村级)*/
        List<Map<String, Object>> getNextAreaTown = indexDisputeEventMapper.getAreaVillage(Id);
        /*再下一级地区(网格级)*/
        List<Map<String, Object>> getNextAgainAreaTown = indexDisputeEventMapper.getAreaHouse(Id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String useName = user.getUserName();
        SysUser sysUser = userService.selectUserByUserName(useName);
        Long areaId = sysUser.getArea();
        if (areaId != null && areaId != 0) {
            if (areaId!=50){
                //areaId不等于50则说明数据为地区数据
                getAreaTown = indexDisputeEventMapper.selectAreaTown(areaId);
            }
        }
        if (level != null && !level.isEmpty()){
            if (level.equals("1")){
                getAreaTownList = getAreaTown;
            }else if (level.equals("2")){
                getAreaTownList = getNextAreaTown;
            }else if (level.equals("3")){
                getAreaTownList = getNextAgainAreaTown;
            }
        }else {
            getAreaTownList = getAreaTown;
        }
        for (Map<String, Object> area : getAreaTownList) {
            // 访问 Map 中的元素
            Long townId = 0l;
            String townName = "";
            if (level != null && !level.isEmpty()){
                if (level.equals("1")){
                    townId = (Long) area.get("townId"); // id;
                    townName = (String) area.get("townName"); // name
                }else if (level.equals("2")){
                    townId = (Long) area.get("villageId"); // id;
                    townName = (String) area.get("villageName"); // name
                }else if (level.equals("3")){
                    townId = (Long) area.get("houseId"); // id;
                    townName = (String) area.get("houseName"); // name
                }
            }else {
                townId = (Long) area.get("townId"); // id;
                townName = (String) area.get("townName"); // name
            }

            int numf = 0;
            int numy = 0;
            int nums = 0;
            int numzs= 0;
            // 进一步处理逻辑
            for (Map.Entry<String, String> entry : ageList.entrySet()) {
                for (String subject : subjectList) {
                String key = entry.getKey();
                String value = entry.getValue();
                /*若key值为1、2、3 则为周期筛选*/
                startTime = null;
                endTime = null;
                if (!key.equals("0")) {
                    startTime = before(value);
                    endTime = after(value);
                }

                /*非纠纷*/
                CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmNotDisputeEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmNotDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmNotDisputeEventVo.setTownId(String.valueOf(townId));
                }
                if (startTime != null && !startTime.isEmpty()) {
                    cmNotDisputeEventVo.setStartTime(startTime);
                    cmNotDisputeEventVo.setEndTime(endTime);
                }
                if (!subject.equals("总数")){
                    if (subject.equals("自然人")){
                        cmNotDisputeEventVo.setApplicantType("1");
                    }else if (subject.equals("法人组织")){
                        cmNotDisputeEventVo.setApplicantType("2");
                    }else if (subject.equals("非法人组织")){
                        cmNotDisputeEventVo.setApplicantType("3");
                    }
                }
                List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
                numf = cmNotDisputeEventVoList.size();
                    if (source.equals("1")){
                        numf = numf;
                    }else if (source.equals("2")){
                        numf = 0;
                    }else if (source.equals("3")){
                        numf = 0;
                    }else {
                        numf = numf;
                    }
                    if (value.equals("纠纷总数")){
                        if (numzs<3){
                            records.add(new DataRecord(townId, townName, value, numf));
                        }
                        numzs+=1;
                    }else {
                        records.add(new DataRecord(townId, townName, value+subject, numf));
                    }


                /*一般纠纷*/
                CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
                cmOrdinaryDisputeEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmOrdinaryDisputeEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmOrdinaryDisputeEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmOrdinaryDisputeEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()) {
                    cmOrdinaryDisputeEventVo.setStartTime(startTime);
                    cmOrdinaryDisputeEventVo.setEndTime(endTime);
                }
                if (!subject.equals("总数")){
                    if (subject.equals("自然人")){
                        cmOrdinaryDisputeEventVo.setApplicantType("1");
                    }else if (subject.equals("法人组织")){
                        cmOrdinaryDisputeEventVo.setApplicantType("2");
                    }else if (subject.equals("非法人组织")){
                        cmOrdinaryDisputeEventVo.setApplicantType("3");
                    }
                }
                List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
                numy = cmOrdinaryDisputeEventVoList.size();
                    if (source.equals("1")){
                        numy = numy;
                    }else if (source.equals("2")){
                        numy = 0;
                    }else if (source.equals("3")){
                        numy = 0;
                    }else {
                        numy = numy;
                    }
                    if (value.equals("纠纷总数")){
                        if (numzs<3){
                            records.add(new DataRecord(townId, townName, value, numy));
                        }
                        numzs+=1;
                    }else {
                        records.add(new DataRecord(townId, townName, value+subject, numy));
                    }

                /*诉前调解件*/
                CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
                cmPreLitigationMediationEventVo.setDraftFlag(0);

                if (level != null && !level.isEmpty()) {
                    if (level.equals("1")) {
                        cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                    } else if (level.equals("2")) {
                        cmPreLitigationMediationEventVo.setVillageId(String.valueOf(townId));
                    } else if (level.equals("3")) {
                        cmPreLitigationMediationEventVo.setHouseId(String.valueOf(townId));
                    }
                } else {
                    cmPreLitigationMediationEventVo.setTownId(String.valueOf(townId));
                }

                if (startTime != null && !startTime.isEmpty()) {
                    cmPreLitigationMediationEventVo.setStartTime(startTime);
                    cmPreLitigationMediationEventVo.setEndTime(endTime);
                }
                if (!subject.equals("总数")){
                    if (subject.equals("自然人")){
                        cmPreLitigationMediationEventVo.setApplicantType("1");
                    }else if (subject.equals("法人组织")){
                        cmPreLitigationMediationEventVo.setApplicantType("2");
                    }else if (subject.equals("非法人组织")){
                        cmPreLitigationMediationEventVo.setApplicantType("3");
                    }
                }
                List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
                nums = cmPreEventVoList.size();
                    if (source.equals("1")){
                        nums = nums;
                    }else if (source.equals("2")){
                        nums = 0;
                    }else if (source.equals("3")){
                        nums = 0;
                    }else {
                        nums = nums;
                    }
                if (value.equals("纠纷总数")){
                    if (numzs<3){
                        records.add(new DataRecord(townId, townName, value, nums));
                    }
                    numzs+=1;
                }else {
                    records.add(new DataRecord(townId, townName, value+subject, nums));
                }

            }
            }

        }

        Map<String, Map<String, Integer>> statistics = new LinkedHashMap<>();
        for (DataRecord record : records) {
            Long id = record.getId();
            String region = record.getRegion();
            String type = record.getType();
            double value = record.getValue();

            statistics.putIfAbsent(region, new LinkedHashMap<>());
            statistics.get(region).put(type, (int) (statistics.get(region).getOrDefault(type, 0) + value));
        }

        List<Object> dataResult=new ArrayList();
        for (Map.Entry<String, Map<String, Integer>> entry : statistics.entrySet()) {
            String region = entry.getKey();
            Map<String, Integer> typeStatistics = entry.getValue();

            // 创建一个用于保存地区信息的 Map
            Map<String, Object> regionData = new LinkedHashMap<>();
            regionData.put("addr", region);
            List<Map<String, Object>> typesData = new ArrayList<>();

            for (Map.Entry<String, Integer> typeEntry : typeStatistics.entrySet()) {
                // 创建一个用于保存每个类型信息的 Map
                Map<String, Object> typeData = new LinkedHashMap<>();
                typeData.put("type", typeEntry.getKey());
                typeData.put("num", typeEntry.getValue());
                typesData.add(typeData);
            }

            // 将类型数据加入地区数据中
            regionData.put("data", typesData);
            // 将地区数据添加到结果列表中
            dataResult.add(regionData);
        }
        return dataResult;
    }

    @Override
    public List<MediatorVo> selectMediatorVoList(MediatorVo mediatorVo)
    {
        return statisticsMapper.selectMediatorVoList(mediatorVo);
    }

    @Override
    public List<MediationTypeVo> selectMediationTypeVoList(MediationTypeVo mediationTypeVo)
    {
        return  statisticsMapper.selectMediationTypeVoList(mediationTypeVo);
    }

    @Override
    public List<MediatorTimeVo> selectMediationTimeVoList(MediatorTimeVo mediatorTimeVo)
    {
        List<MediatorTimeVo> list = statisticsMapper.selectMediationTimeVoList(mediatorTimeVo);
        List<MediatorTimeVo> mediatorTimeVoList = new ArrayList<>();
        for (MediatorTimeVo timeVo : list){
            MediatorTimeVo mediatorTimeVo1 = new MediatorTimeVo();
            mediatorTimeVo1.setUserId(timeVo.getUserId());
            mediatorTimeVo1.setName(timeVo.getName());
            mediatorTimeVo1.setAddr(timeVo.getAddr());
            mediatorTimeVo1.setTotalNum(timeVo.getTotalNum());
            mediatorTimeVo1.setSuccessRate(timeVo.getSuccessRate());
            //根据获取的userId查询对应的案件信息并获取对应的操作记录  获取分派和结案的操作时间点
            CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
            cmOrdinaryDisputeEventVo.setMediatorCommitteeUserId(timeVo.getUserId());
            List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoList(cmOrdinaryDisputeEventVo);
            List<Duration> processingTimes = new ArrayList<>();
            LocalDateTime[] startTimes = {};
            LocalDateTime[] endTimes ={};
            ArrayList<LocalDateTime> startTimesList = new ArrayList<>();
            ArrayList<LocalDateTime> endTimesList = new ArrayList<>();
            for (CmOrdinaryDisputeEventVo c :cmOrdinaryDisputeEventVoList){
                CmOperatingRecordVo cmOperatingRecordVo = new CmOperatingRecordVo();
                cmOperatingRecordVo.setEventId(c.getId());
                //获取的案件操作记录  必须是有结案完成之后才算时间的量
                cmOperatingRecordVo.setOperatingType(9);
                List<CmOperatingRecordVo> cmOperatingRecordVostartTimeList = statisticsMapper.selectCmOperatingRecordList(cmOperatingRecordVo);
                if (cmOperatingRecordVostartTimeList.size()>0){
                    //说明这个数据已完成结案操作  再去执行列表操作
                    List<CmOperatingRecordVo> cmOperatingRecordVoList = statisticsMapper.selectCmOperatingRecordList(cmOperatingRecordVo);
                    for (CmOperatingRecordVo recordVo :cmOperatingRecordVoList){
                        Date startTime = new Date() ;
                        Date endTime = new Date();
                        if (recordVo.getOperatingType()==8){
                            startTime = recordVo.getOperatingTime();
                            startTimesList.add(startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                        }
                        if (recordVo.getOperatingType()==9){
                            endTime = recordVo.getOperatingTime();
                            endTimesList.add(endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                        }

                    }
                }
            }

            startTimes = startTimesList.toArray(new LocalDateTime[0]);
            endTimes = endTimesList.toArray(new LocalDateTime[0]);
            //计算每个案件的处理时间，并存储在 List 中
            for (int i =0; i < startTimes.length; i++) {
                Duration duration = Duration.between(startTimes[i], endTimes[i]);
                processingTimes.add(duration);
            }
            if (processingTimes.size()>0){
                // 找到最长和最短的处理时间
                Duration longestDuration = processingTimes.get(0);
                Duration shortestDuration = processingTimes.get(0);

                for (Duration duration : processingTimes) {
                    if (duration.compareTo(longestDuration) >0) {
                        longestDuration = duration;
                    }
                    if (duration.compareTo(shortestDuration) <0) {
                        shortestDuration = duration;
                    }
                }
                //计算总处理时间
                Duration totalProcessingTime = Duration.ZERO;
                for (Duration duration : processingTimes) {
                    totalProcessingTime = totalProcessingTime.plus(duration);
                }

                //计算平均处理时间
                Duration averageProcessingTime = totalProcessingTime.dividedBy(processingTimes.size());
                String chang = longestDuration.toDays()+"天"+longestDuration.toHours() %24+"时"+longestDuration.toMinutes() %60+"分";
                String duan = shortestDuration.toDays()+"天"+shortestDuration.toHours() %24+"时"+shortestDuration.toMinutes() %60+"分";
                String avg = averageProcessingTime.toDays() + "天" + averageProcessingTime.toHours() %24 + "时" + averageProcessingTime.toMinutes() %60 + "分";

                System.out.printf("最长处理时间: %d天 %d时 %d分%n", longestDuration.toDays(), longestDuration.toHours() %24, longestDuration.toMinutes() %60);
                System.out.printf("最短处理时间: %d天 %d时 %d分%n", shortestDuration.toDays(), shortestDuration.toHours() %24, shortestDuration.toMinutes() %60);
                System.out.printf("平均处理时间: %d天 %d时 %d分%n", averageProcessingTime.toDays(), averageProcessingTime.toHours() %24, averageProcessingTime.toMinutes() %60);
                mediatorTimeVo1.setLongTime(chang);
                mediatorTimeVo1.setShortTime(duan);
                mediatorTimeVo1.setAverageTime(avg);
            }else {
                mediatorTimeVo1.setLongTime("0");
                mediatorTimeVo1.setShortTime("0");
                mediatorTimeVo1.setAverageTime("0");
            }

            mediatorTimeVoList.add(mediatorTimeVo1);
        }
        return mediatorTimeVoList;
    }


    public static class DataRecord {
        private Long id;
        private String region;
        private String type;
        private int value;

        public DataRecord(Long id, String region, String type, int value) {
            this.id = id;
            this.region = region;
            this.type = type;
            this.value = value;
        }

        public Long getId() {
            return id;
        }

        public String getRegion() {
            return region;
        }

        public String getType() {
            return type;
        }

        public int getValue() {
            return value;
        }
    }

    public static class DataTypeRecord {
        private Long id;
        private String region;
        private String type;
        private List<TypeRecord>  typeRecordList;

        public DataTypeRecord(Long id, String region, String type, List<TypeRecord>  typeRecordList) {
            this.id = id;
            this.region = region;
            this.type = type;
            this.typeRecordList = typeRecordList;
        }

        public Long getId() {
            return id;
        }

        public String getRegion() {
            return region;
        }

        public String getType() {
            return type;
        }

        public List<TypeRecord>  getTypeRecordList() {
            return typeRecordList;
        }
    }

    public static class TypeRecord {
        private Long id;
        private String name;
        private int num;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }



    //根据查询的状态查询数量
    public Map<String,Integer> StatusInquiryNum(Integer state,String type,String level,int typeTwo,String startTime,String endTime,String num,String areaId){

        Map<String,Integer> map =new LinkedHashMap<>();
        if (type.equals("1")){
            CmNotDisputeEventVo cmNotDisputeEventVo = new CmNotDisputeEventVo();
            cmNotDisputeEventVo.setDraftFlag(0);
            if (state!=999){
                cmNotDisputeEventVo.setEventState(state);
            }
            if (typeTwo!=999){
                cmNotDisputeEventVo.setEventType(typeTwo);
            }
            if (startTime != null && !startTime.isEmpty()){
                cmNotDisputeEventVo.setStartTime(startTime);
                cmNotDisputeEventVo.setEndTime(endTime);
            }
            List<CmNotDisputeEventVo> cmNotDisputeEventVoList = statisticsMapper.cmNotDisputeEventVoTwoList(cmNotDisputeEventVo);
            for (CmNotDisputeEventVo event : cmNotDisputeEventVoList) {
                String region = "";
                if (num.equals("1")){
                    if (level.equals("1")){
                        region = event.getTownName();
                    }else if (level.equals("2")){
                        region =event.getVillageName();
                    }else if (level.equals("3")){
                        region =event.getHouseName();
                    }
                }else if(num.equals("2")) {
                    region =event.getTypeName();
                    if (areaId != null && !areaId.isEmpty()) {
                        // 字符串不为 null 且不为空
                        cmNotDisputeEventVo.setAreaId(areaId);
                    }
                }

                map.put(region, map.getOrDefault(region, 0) + 1);
            }
        }else if (type.equals("2")){
            CmOrdinaryDisputeEventVo cmOrdinaryDisputeEventVo = new CmOrdinaryDisputeEventVo();
            cmOrdinaryDisputeEventVo.setDraftFlag(0);
            if (state!=999){
                cmOrdinaryDisputeEventVo.setEventState(state);
            }
            if (typeTwo!=999){
                cmOrdinaryDisputeEventVo.setEventType(typeTwo);
            }
            if (startTime != null && !startTime.isEmpty()){
                cmOrdinaryDisputeEventVo.setStartTime(startTime);
                cmOrdinaryDisputeEventVo.setEndTime(endTime);
            }
            List<CmOrdinaryDisputeEventVo> cmOrdinaryDisputeEventVoList = statisticsMapper.cmOrdinaryDisputeEventVoTwoList(cmOrdinaryDisputeEventVo);
            for (CmOrdinaryDisputeEventVo disputeEventVo : cmOrdinaryDisputeEventVoList) {
                String region = "";
                if (num.equals("1")){
                    if (level.equals("1")){
                        region = disputeEventVo.getTownName();
                    }else if (level.equals("2")){
                        region =disputeEventVo.getVillageName();
                    }else if (level.equals("3")){
                        region =disputeEventVo.getHouseName();
                    }
                }
                else if (num.equals("2")){
                    region =disputeEventVo.getTypeName();
                    if (areaId != null && !areaId.isEmpty()) {
                        // 字符串不为 null 且不为空
                        cmOrdinaryDisputeEventVo.setAreaId(areaId);
                    }
                }

                map.put(region, map.getOrDefault(region, 0) + 1);
            }
        }else if (type.equals("3")){
            CmPreLitigationMediationEventVo cmPreLitigationMediationEventVo = new CmPreLitigationMediationEventVo();
            cmPreLitigationMediationEventVo.setDraftFlag(0);
            if (state!=999){
                cmPreLitigationMediationEventVo.setEventState(state);
            }
            if (typeTwo!=999){
                cmPreLitigationMediationEventVo.setEventType(typeTwo);
            }
            if (startTime != null && !startTime.isEmpty()){
                cmPreLitigationMediationEventVo.setStartTime(startTime);
                cmPreLitigationMediationEventVo.setEndTime(endTime);
            }
            List<CmPreLitigationMediationEventVo> cmPreEventVoList = statisticsMapper.CmPreLitigationMediationEventVoTwoList(cmPreLitigationMediationEventVo);
            for (CmPreLitigationMediationEventVo preLitigationMediationEventVo : cmPreEventVoList) {
                String region = "";
                if (num.equals("1")){
                    if (level.equals("1")){
                        region = preLitigationMediationEventVo.getTownName();
                    }else if (level.equals("2")){
                        region =preLitigationMediationEventVo.getVillageName();
                    }else if (level.equals("3")){
                        region =preLitigationMediationEventVo.getHouseName();
                    }
                }
                else if (num.equals("2")){
                    region =preLitigationMediationEventVo.getTypeName();
                    if (areaId != null && !areaId.isEmpty()) {
                        // 字符串不为 null 且不为空
                        preLitigationMediationEventVo.setAreaId(areaId);
                    }
                }

                map.put(region, map.getOrDefault(region, 0) + 1);
            }
        }

        return map;
    }

    // 合并方法
    private static void mergeMaps(Map<String, Integer> combinedMap, Map<String, Integer> sourceMap) {
        for (Map.Entry<String, Integer> entry : sourceMap.entrySet()) {
            combinedMap.put(entry.getKey(), combinedMap.getOrDefault(entry.getKey(), 0) + entry.getValue());
        }
    }

    // 合并方法
    private static void mergeToMap(Map<String, Object> combinedMap, Map<String, Integer> sourceMap) {
        // 记录所有可能的键，以确保合并后存在所有键
        Set<String> allKeys = new HashSet<>(combinedMap.keySet());
        allKeys.addAll(sourceMap.keySet());

        for (String key : allKeys) {
            Integer value = sourceMap.get(key);

            // 检查 sourceMap 是否有值，如果没有则设置为 0
            if (value == null) {
                value = 0; // 设为 0
            }

            // 如果键已经存在，则将值更新为列表或添加到列表中
            if (combinedMap.containsKey(key)) {
                Object existingValue = combinedMap.get(key);
                if (existingValue instanceof List) {
                    ((List<Integer>) existingValue).add(value); // 添加到已有的列表
                } else {
                    List<Integer> valueList = new ArrayList<>();
                    valueList.add((Integer) existingValue); // 将原有值加入列表
                    valueList.add(value); // 添加新值
                    combinedMap.put(key, valueList); // 更新为列表
                }
            } else {
                combinedMap.put(key, value); // 新键直接添加
            }
        }
    }

    //日期循环方法
    public static List<LocalDate> getLastNDays(int days, LocalDate endDate) {
        List<LocalDate> dates = new ArrayList<>();
        for (int i = 0; i < days; i++) {
            dates.add(endDate.minusDays(i));
        }
        return dates;
    }


    public static String getCurrentWeekRange(LocalDate today) {
        LocalDate startOfWeek = today.with(java.time.DayOfWeek.MONDAY);
        LocalDate endOfWeek = today.with(java.time.DayOfWeek.SUNDAY);
        return formatDateRange(startOfWeek, endOfWeek);
    }

    public static String getLastWeekRange(LocalDate today) {
        LocalDate startOfPrevWeek = today.minusWeeks(1).with(java.time.DayOfWeek.MONDAY);
        LocalDate endOfPrevWeek = today.minusWeeks(1).with(java.time.DayOfWeek.SUNDAY);
        return formatDateRange(startOfPrevWeek, endOfPrevWeek);
    }

    public static String getLastTwoWeeksRange(LocalDate today) {
        // 获取前两周的开始日期（上上周的周一）
        LocalDate startOfTwoWeeksAgo = today.minusWeeks(2).with(java.time.DayOfWeek.MONDAY);
        // 获取前一周的结束日期（上周的周日）
        LocalDate endOfPrevWeek = today.minusWeeks(2).with(java.time.DayOfWeek.SUNDAY);
        return formatDateRange(startOfTwoWeeksAgo, endOfPrevWeek);
    }

    public static String getCurrentMonthRange(LocalDate today) {
        LocalDate startOfMonth = today.withDayOfMonth(1);
        LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
        return formatDateRange(startOfMonth, endOfMonth);
    }

    public static String getLastMonthRange(LocalDate today) {
        LocalDate startOfPrevMonth = today.minusMonths(1).withDayOfMonth(1);
        LocalDate endOfPrevMonth = today.minusMonths(1).withDayOfMonth(today.minusMonths(1).lengthOfMonth());
        return formatDateRange(startOfPrevMonth, endOfPrevMonth);
    }

    public static String getLastTwoMonthsRange(LocalDate today) {
        LocalDate startOfTwoMonthsAgo = today.minusMonths(2).withDayOfMonth(1);
        LocalDate localDate = today.minusMonths(2);
        LocalDate endOfPrevMonth = localDate.withDayOfMonth(today.minusMonths(1).getDayOfMonth());
//        LocalDate endOfPrevMonth = today.minusMonths(2).withDayOfMonth(today.minusMonths(1).lengthOfMonth());
        return formatDateRange(startOfTwoMonthsAgo, endOfPrevMonth);
    }

    public static String getCurrentYearRange(LocalDate today) {
        LocalDate startOfCurrentYear = today.withDayOfYear(1); // 当前年的第一天
        LocalDate endOfCurrentYear = today.withDayOfYear(today.lengthOfYear()); // 当前年的最后一天
        return formatDateRange(startOfCurrentYear, endOfCurrentYear);
    }

    public static String getLastYearRange(LocalDate today) {
        LocalDate startOfLastYear = today.minusYears(1).withDayOfYear(1); // 前一年的第一天
        LocalDate endOfLastYear = today.minusYears(1).withDayOfYear(today.minusYears(1).lengthOfYear()); // 前一年的最后一天
        return formatDateRange(startOfLastYear, endOfLastYear);
    }

    public static String getTwoYearsAgoRange(LocalDate today) {
        LocalDate startOfTwoYearsAgo = today.minusYears(2).withDayOfYear(1); // 前两年的第一天
        LocalDate endOfTwoYearsAgo = today.minusYears(2).withDayOfYear(today.minusYears(2).lengthOfYear()); // 前两年的最后一天
        return formatDateRange(startOfTwoYearsAgo, endOfTwoYearsAgo);
    }

    private static String formatDateRange(LocalDate start, LocalDate end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return start.format(formatter) + " 到 " + end.format(formatter);
    }




    private static String before(String args) {
        String before = "";
        int index = args.indexOf("到");
        if (index != -1) {
            before = args.substring(0, index).trim(); // "2024-10-14"
            String after = args.substring(index + 2).trim(); // "2024-10-20"
            System.out.println("开始日期: " + before);
            System.out.println("结束日期: " + after);
        } else {
            System.out.println("字符串格式不正确");
        }
        return before;
    }

    private static String after(String args) {
        String after = "";
        int index = args.indexOf("到");
        if (index != -1) {
            after = args.substring(index + 2).trim(); // "2024-10-20"
            System.out.println("结束日期: " + after);
        } else {
            System.out.println("字符串格式不正确");
        }
        return after;
    }
}
